package com.adminkk.servlet;

import com.adminkk.handler.HandlerMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DispatcherServlet",loadOnStartup=1,urlPatterns={"/"})
public final  class DispatcherServlet extends HttpServlet {



    public static final String BASE_SCAN_URL = "com.adminkk";

    //初始化容器
    @Override
    public void init() throws ServletException {
        doInit();
    }

    //处理业务请求
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doService(req,resp);
    }

    private void doService(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
        try {
            HandlerMapping.doService(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    private void doInit() throws ServletException {
        try {
            HandlerMapping.doInit(this.BASE_SCAN_URL);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }

    }
}
